//
//  UIImage+TMimageCloor.m
//  国学商家管理
//
//  Created by mac on 2018/6/19.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "UIImage+TMimageCloor.h"

@implementation UIImage (TMimageCloor)
+(UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); //宽高 1.0只要有值就够了
    UIGraphicsBeginImageContext(rect.size); //在这个范围内开启一段上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);//在这段上下文中获取到颜色UIColor
    CGContextFillRect(context, rect);//用这个颜色填充这个上下文
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//从这段上下文中获取Image属性,,,结束
    UIGraphicsEndImageContext();
    
    return image;
}
+(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = size.width;
    CGFloat targetHeight = size.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    
    if(CGSizeEqualToSize(imageSize, size) == NO){
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        //        NSLog(@"scuor%f---scour%f---show%f---%f---%f---%f", width, height, scaledWidth, scaledHeight, widthFactor, heightFactor);
        
        
        if(widthFactor > heightFactor){
            scaleFactor = widthFactor;
        }else{
            scaleFactor = heightFactor;
        }
        scaledWidth = width * scaleFactor;//748*0.5
        scaledHeight = height * scaleFactor;//0.5*382
        
        if(widthFactor > heightFactor){
            thumbnailPoint.y = (targetHeight - scaledHeight) *0.5;
        }else if(widthFactor < heightFactor){
            thumbnailPoint.x = (targetWidth - scaledWidth) *0.5;
        }
    }
    
    //    UIGraphicsBeginImageContext(size);模糊 与高清原图
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    //    [sourceImage drawInRect:CGRectMake(0, 0, scaledWidth, scaledHeight)];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    if(newImage == nil){
        NSLog(@"scale image fail");
    }
    UIGraphicsEndImageContext();
    return newImage;
}
+ (UIImage *)clipImgWithName:(UIImage *)name {
    
    CGFloat width = name.size.width;
    CGFloat height = name.size.height;
    //开始绘制图片
    UIGraphicsBeginImageContext(name.size);
    CGContextRef gc = UIGraphicsGetCurrentContext();
    
    CGContextTranslateCTM(gc, 0, height);
    CGContextScaleCTM(gc, 1, -1);
    CGContextSetStrokeColorWithColor(gc, [UIColor redColor].CGColor);//设置线的颜色
    CGContextSetLineWidth(gc, 1);
    CGContextMoveToPoint(gc, width/2, 0);
    CGContextAddLineToPoint(gc, width, height/2);
    CGContextAddLineToPoint(gc, width/2, height);
    CGContextAddLineToPoint(gc, 0, height/2);
    CGContextClosePath(gc);
    CGContextClip(gc);
    //    CGContextStrokePath(gc);
    CGContextDrawImage(gc, CGRectMake(0, 0, width, height), [name CGImage]);
    //结束绘画
    UIImage *destImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return destImg;
}
+ (UIImage *)drawling :(UIImage *)name{
    UIImage *image = [self clipImgWithName:name];
    CGFloat width = image.size.width-5;
    CGFloat height = image.size.height-5;
    UIGraphicsBeginImageContext(image.size);
    CGContextRef gc = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(gc, 0, height);
    CGContextScaleCTM(gc, 1, -1);
    CGContextSetStrokeColorWithColor(gc, [UIColor whiteColor].CGColor);//设置线的颜色
    CGContextSetLineWidth(gc, 5);
    CGContextMoveToPoint(gc, width/2, 0);
    CGContextAddLineToPoint(gc, width, height/2);
    CGContextAddLineToPoint(gc, width/2, height);
    CGContextAddLineToPoint(gc, 0, height/2);
    CGContextAddLineToPoint(gc, width/2, 0);
    CGContextStrokePath(gc);
    CGContextDrawImage(gc, CGRectMake(0, 0, width, height), [image CGImage]);
    //结束绘画
    UIImage *destImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return destImg;
}
@end
